package com.dzzw.Dao;

import com.dzzw.Model.Admin;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//管理员
public class AdminDao {
    private final Connection con = DButils.getCon();
    //管理员登录;
    public Admin AdminLogin(String admin_actnumber,String admin_password,String admin_name) {
        Admin admin = new Admin();
        String sql = "select * from t_admin where admin_actnumber=? and admin_password=?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,admin_actnumber);
            preparedStatement.setString(2,admin_password);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                admin.setAdmin_id(resultSet.getInt("admin_id"));
                admin.setAdmin_name(resultSet.getString("admin_name"));
                admin.setAdmin_actnumber(resultSet.getString("admin_actnumber"));
                admin.setAdmin_password(resultSet.getString("admin_password"));
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
        } catch(Exception e){
            System.out.println("数据查询失败！");

            e.printStackTrace();
        }
        return admin;
    }

    //管理员信息添加;
    public void AdminAdd(Admin admin) {
        String sql = "insert into t_admin(admin_id,admin_name,admin_password,admin_actnumber) values(?,?,?,?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,admin.getAdmin_id());
            preparedStatement.setString(2, admin.getAdmin_name());
            preparedStatement.setString(3, admin.getAdmin_password());
            preparedStatement.setString(4, admin.getAdmin_actnumber());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //管理员信息修改;
    public void AdminModify(Admin admin) {
        String sql = "update t_admin set admin_actnumber=?,admin_name=?,admin_password=? where admin_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,admin.getAdmin_actnumber());
            preparedStatement.setString(2, admin.getAdmin_name());
            preparedStatement.setString(3, admin.getAdmin_password());
            preparedStatement.setInt(4,admin.getAdmin_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据修改失败！");

            e.printStackTrace();
        }
    }

    //管理员信息单条注销;
    public void AdminDelete(int admin_id,String admin_actnumber) {
        String sql = "Delete from t_admin where admin_id = ? and admin_actnumber = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,admin_id);
            preparedStatement.setString(2,admin_actnumber);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据单条删除失败！");

            e.printStackTrace();
        }
    }

    //查询所有管理员数据;
    public static List<Admin> FindAllAdmin() {
        List<Admin> list = new ArrayList<>();
        String sql = "select * from t_admin";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Admin admin = new Admin();
                admin.setAdmin_id(resultSet.getInt(1));
                admin.setAdmin_actnumber(resultSet.getString(2));
                admin.setAdmin_name(resultSet.getString(3));
                admin.setAdmin_password(resultSet.getString(4));
                list.add(admin);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return list;
    }

    //根据admin_id查询管理员信息;
    public static List<Admin> FindAdminById(int admin_id) {
        List<Admin> adminlist = new ArrayList<>();
        String sql = "select * from t_admin where admin_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,admin_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Admin admin = new Admin();
                admin.setAdmin_id(resultSet.getInt(1));
                admin.setAdmin_actnumber(resultSet.getString(2));
                admin.setAdmin_name(resultSet.getString(3));
                admin.setAdmin_password(resultSet.getString(4));
                adminlist.add(admin);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return adminlist;
    }

    //数据批量注销;

    //数据分页查询;

    //分页查询所有管理员;

}
